<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-18 18:14
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class getAppointmentListCriteria extends BaseCriterias
{
	protected $data;

	public function __construct($data)
	{
		$this->data = $data;
	}

	public function apply($model, RepositoryInterface $repository)
	{
		$map = [
			'appointment.member_id' => $this->data['member_id'],
			'appointment.status'  => $this->data['status']
		];


//		DATE_FORMAT(FROM_UNIXTIME(hx_appointment.appointment_time),'%Y-%m-%d %T')  as appointmentTime,
		$select = '
			hx_appointment.id,
			hx_appointment.store_id as storeId,
			hx_appointment.barber_id as barberId,
			hx_appointment.service_id as serviceId,
			hx_store_service.name as serviceName,
			hx_appointment.member_id as memberId,
			hx_appointment.appointment_time as appointmentTime,
			hx_appointment.member_card as memberCard,
			hx_appointment.status,
			IF(ISNULL(hx_appointment.`price`),0.00,hx_appointment.`price`) as `price`,
			hx_appointment.created_at as createTime,
			hx_store.name as storeName,
			hx_store.address,
			hx_store.doorplate,
			hx_user.nick as barberName,
			hx_user.mobile as barberMobile,
			hx_picture.image as barberFace,
			hx_order.order_no as orderNo
		';

		return $model->selectRaw($select)
			->leftJoin('store_service','appointment.service_id','store_service.id')
			->leftJoin('store',function($join){
				return $join->on('appointment.store_id', '=', 'store.id');
			})
			->leftJoin('order','appointment.id','order.appointment_id')
			->leftJoin('user','appointment.barber_id','user.id')
			->leftJoin('picture','user.portrait','picture.id')
			->where($map)->orderBy('appointment.id','desc');
	}
}